package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.dao.OrderDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrdersettingService;
import com.itheima.service.ValidateCodeService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hql
 * @date 2019-12-08 17:25
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl  implements OrderService {
    @Autowired
   private MemberService memberService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private  JedisPool jedisPool;

    @Autowired
    private OrdersettingService ordersettingService;
    @Override
    public Result addMember(Map<String,String> map) throws Exception {
        String telphone = map.get("telephone");
        String validateCode = map.get("validateCode");
        String orderDate = map.get("orderDate");
        String idCard = map.get("idCard");
        String name = map.get("name");
        String sex = map.get("sex");
        String setmealId = map.get("setmealId");
        String orderType = map.get("orderType");
        Jedis jedis = jedisPool.getResource();
        //调用方法判断验证码的正确性
 /*      String s= jedis.get(telphone+ RedisMessageConstant.SENDTYPE_ORDER);
        if (!(validateCode.equals(s))){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }*/
    if (!validateCodeService.checkValidateCode(telphone,validateCode,telphone+ RedisMessageConstant.SENDTYPE_ORDER)){
        return new Result(false, MessageConstant.VALIDATECODE_ERROR);
    }
        //判断当天是否可以进行预约，并返回当天的预约情况
        OrderSetting orderSetting= ordersettingService.findByOrderSetting(orderDate);
        //如果不存在，则说明不能进行预约
        if (orderSetting==null){
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断已预约人数跟最高预约人数的大小
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //判断是否存在member会员
        Member member = memberService.checkTelphone(telphone);
        Integer id =null;
        if (member==null){
            Member meber2 =new Member(name,sex,idCard,telphone,new Date());
            memberService.addMember(meber2);
            id=meber2.getId();
        }else {
           id = member.getId();
        }
        //判断同一用户是否重复预约
        Order order= orderService.checkOrder(setmealId,id,orderDate);
        if (order!=null){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        //设置order的值
        order=new Order();
        order.setMemberId(id);
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderStatus("未预约");
        order.setOrderType(orderType);
        order.setSetmealId(Integer.parseInt(setmealId));
        //添加信息到Order表
        orderDao.addOrder(order);
        //g更新OrderSetting表中的信息
        orderSetting.setReservations(orderSetting.getReservations()+1);
         ordersettingService.updateOrderSetting(orderSetting);
        //发送成功短信
      //  SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telphone, ValidateCodeUtils.generateValidateCode4String(4));
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,order.getId());
    }

    /**
     * 判断是否有相同的预约
     * @param setmealId
     * @param id
     * @param orderDate
     * @return
     */
    @Override
    public Order checkOrder(String setmealId, Integer id, String orderDate) {
        Order order = orderDao.checkOrder(setmealId,id,orderDate);
        return order;
    }

    /**
     * 返回前台展示的预约的信息
     * @param id 预约的id
     * @return
     */
    @Override
    public Map<String, String> findById(Integer id) {
        Map<String, String> map =  orderDao.findById(id);
        return map;
    }

    /**
     *拿到今天预约的人数
     * @param reportDate
     * @return
     */
    @Override
    public Integer getTodayOrderNumber(String reportDate) {
       Integer count =  orderDao.getTodayOrderNumber(reportDate);
        return count;
    }

    /**
     * 拿到今天就诊的人数
     * @param reportDate
     * @return
     */
    @Override
    public Integer getTodayVisitsNumber(String reportDate) {
        Integer count = orderDao.getTodayVisitsNumber(reportDate);
        return count;
    }

    /**
     * 拿到这周预约的人数
     * @param firstDayOfWeek
     * @return
     */
    @Override
    public Integer getThisWeekOrderNumber(String firstDayOfWeek) {
        Integer count = orderDao.getThisWeekOrderNumber(firstDayOfWeek);
        return count;
    }

    /**
     * 这周到诊的人数
     * @param firstDayOfWeek
     * @return
     */
    @Override
    public Integer getThisWeekVisitsNumber(String firstDayOfWeek) {
        Integer count =orderDao.getThisWeekVisitsNumber(firstDayOfWeek);
        return count;
    }

    /**
     * 拿到这个月的预约数
     * @param firstDayOfMonth
     * @return
     */
    @Override
    public Integer getThisMonthOrderNumber(String firstDayOfMonth) {
        Integer count =orderDao.getThisMonthOrderNumber(firstDayOfMonth);
        return count;
    }

    /**
     * 拿到热门的套餐信息
     * @return
     */
    @Override
    public List<Map<String, Object>> findHotSetmealList() {
        List<Map<String, Object>> mapList = orderDao.findHotSetmealList();
        return mapList;
    }

}
